//
//  YWMaterialManager.m
//  maintain
//
//  Created by 烟雨云渊 on 16/9/1.
//  Copyright © 2016年 ewin. All rights reserved.
//

#import "YWMaterialManager.h"
#import "WMHttpTool.h"
#import "YWCommonTool.h"
#import "YWConst.h"
#import "YWMacro.h"
#import "MJExtension.h"

#import "YWMaterialStock.h"
#import "YWMaterialRelation.h"
#import "YWMaterialApply.h"
#import "YWMaterialStockOut.h"
#import "YWMaterialReturn.h"
#import "YWMaterialDBManager.h"
#import "YWHandler.h"
#import "YWBuilding.h"
#import "YWBuildingManager.h"
#import "YWLoginTool.h"
#import "YWLoginUser.h"
#import "YWReply.h"
#import "YWReplyManager.h"
#import "YWMaterialReturnDetail.h"
#import "YWMaterialStockOutDetail.h"

@implementation YWMaterialManager
YWSingletonM(Manager)

#pragma mark - 库存表的更新、取值

- (BOOL)updateMaterialStockWithArray:(NSArray*)materialStockArray
{
    return [[YWMaterialDBManager shareManager] updateMaterialStockWithArray:materialStockArray];
}
- (NSArray *)materialStockWithPage:(NSInteger)page
{
    return [[YWMaterialDBManager shareManager] materialStockWithPage:page];
}
#pragma mark - 更新物料信息表的更新、取值
- (BOOL)updateMaterialInfoWithArray:(NSArray*)materialInfoArray
{
    return [[YWMaterialDBManager shareManager] updateMaterialInfoWithArray:materialInfoArray];
}
//- (YWMaterialApply *)materialApplyWithMaterialInfoId:(NSInteger)materialInfoId
//{
//    
//}
#pragma mark - 物料申请表的更新、取值
//更新物料数据状态
- (BOOL)updateMaterialStatus:(NSInteger)status withApplyId:(NSInteger)applyId
{
    return [[YWMaterialDBManager shareManager] updateMaterialStatus:status withApplyId:applyId];
}

- (BOOL)deleteMaterialApplyWithApplyId:(NSInteger)applyId{
    return [[YWMaterialDBManager shareManager] deleteMaterialApplyWithApplyId:applyId];
}

//更新物料申请状态
- (BOOL)updateMaterialApplyStatus:(NSInteger)applyStatus withApplyId:(NSInteger)applyId
{
    return [[YWMaterialDBManager shareManager] updateMaterialApplyStatus:applyStatus withApplyId:applyId];
}

- (BOOL)updateMaterialApplyWithArray:(NSArray*)materialApplyArray
{
    return [[YWMaterialDBManager shareManager] updateMaterialApplyWithArray:materialApplyArray];
}
- (NSArray *)materialApplyWithPage:(NSInteger)page
{
    return [[YWMaterialDBManager shareManager] materialApplyWithPage:page];
}
- (YWMaterialApplyViewModel *)materialApplyWithMaterialApplyId:(NSInteger)materialApplyId
{
    return [[YWMaterialDBManager shareManager] materialApplyWithMaterialApplyId:materialApplyId];
}
- (YWMaterialApplyViewModel *)materialApplyWithMaterialStockOutId:(NSInteger)materialStockOutId{
    return [[YWMaterialDBManager shareManager] materialApplyWithMaterialStockOutId:materialStockOutId];
}
#pragma mark - 物料申请明细表的更新、取值
- (BOOL)updateMaterialApplyDetailWithArray:(NSArray*)materialApplyDetailArray
{
    return [[YWMaterialDBManager shareManager] updateMaterialApplyDetailWithArray:materialApplyDetailArray];
}

- (YWMaterialApplyDetail *)materialApplyDetailWithMaterialApplyDetailId:(NSInteger)materialApplyDetailId
{
    return [[YWMaterialDBManager shareManager] materialApplyDetailWithMaterialApplyDetailId:materialApplyDetailId];
}
#pragma mark - 物料领用表的更新、取值

- (BOOL)deleteMaterialStockOutWithStockOutId:(NSInteger)stockOutId{
    return [[YWMaterialDBManager shareManager] deleteMaterialStockOutWithStockOutId:stockOutId];
}

- (BOOL)updateMaterialStockOutStatusWithStockOutId:(NSInteger)stockOutId
{
    return [[YWMaterialDBManager shareManager] updateMaterialStockOutStatusWithStockOutId:stockOutId];
}
- (BOOL)updateMaterialReceipientWithArray:(NSArray*)materialReceipientArray
{
    return [[YWMaterialDBManager shareManager] updateMaterialReceipientWithArray:materialReceipientArray];
}
- (NSArray *)materialReceipientWithPage:(NSInteger)page
{
    return [[YWMaterialDBManager shareManager] materialReceipientWithPage:page];
}
- (YWMaterialStockOut*)materialReceipientWithMaterialReceipientId:(NSInteger)materialReceipientId
{
    return [[YWMaterialDBManager shareManager] materialReceipientWithMaterialReceipientId:materialReceipientId];
}
#pragma mark - 物料领用明细表的更新、取值
- (BOOL)updateMaterialReceipientDetailWithArray:(NSArray*)materialReceipientDetailArray
{
    return [[YWMaterialDBManager shareManager] updateMaterialReceipientDetailWithArray:materialReceipientDetailArray];
}
#pragma mark - 物料归还表的更新、取值
- (BOOL)updateMaterialReturnWithArray:(NSArray*)materialReturnArray
{
    return [[YWMaterialDBManager shareManager] updateMaterialReturnWithArray:materialReturnArray];
}

- (BOOL)updateMaterialReturnStatusWithMaterialReturnId:(NSInteger)returnId
{
    return [[YWMaterialDBManager shareManager] updateMaterialReturnStatusWithMaterialReturnId:returnId];
}

- (BOOL)deleteMaterialReturnWithReturnId:(NSInteger)returnId{
    return [[YWMaterialDBManager shareManager] deleteMaterialReturnWithReturnId:returnId];
}

- (NSArray*)materialReturnWithPage:(NSInteger)page
{
    return [[YWMaterialDBManager shareManager] materialReturnWithPage:page];
}
- (YWMaterialReturn*)materialReturnWithMaterialReturnId:(NSInteger)materialReturnId
{
    return [[YWMaterialDBManager shareManager] materialReturnWithMaterialReturnId:materialReturnId];
}
- (BOOL)updateMaterialReturnDetailWithArray:(NSArray*)materialReturnDetailArray
{
    return [[YWMaterialDBManager shareManager] updateMaterialReturnDetailWithArray:materialReturnDetailArray];
}
- (BOOL)updateMaterialCirculationInfoWithArray:(NSArray*)circulationInfoArray{
    return [[YWMaterialDBManager shareManager] updateMaterialCirculationInfoWithArray:circulationInfoArray];
}
#pragma mark - 物料关系表的更新、取值
- (BOOL)updateMaterialRelationWithArray:(NSArray *)materialRelationArray
{
    return [[YWMaterialDBManager shareManager] updateMaterialRelationWithArray:materialRelationArray];
}
- (YWMaterialRelation*)materialRelationWithMaterialApplyId:(NSInteger)materialApplyId
{
    return [[YWMaterialDBManager shareManager] materialRelationWithMaterialApplyId:materialApplyId];
}
- (YWMaterialRelation*)materialRelationWithMaterialReceipientId:(NSInteger)materialReceipientId
{
    return [[YWMaterialDBManager shareManager] materialRelationWithMaterialReceipientId:materialReceipientId];
}
- (YWMaterialRelation*)materialRelationWithMaterialReturnId:(NSInteger)materialReturnId
{
    return [[YWMaterialDBManager shareManager] materialRelationWithMaterialReturnId:materialReturnId];
}
- (NSArray *)materialCirculationInfoArrayWithMaterialRelationId:(NSInteger)materialRelationId{
    return [[YWMaterialDBManager shareManager] materialCirculationInfoArrayWithMaterialRelationId:materialRelationId];
}
#pragma mark - 网络接口请求
- (void)requestMaterialApplyListInfoPage:(NSInteger)page Status:(NSInteger)status WithComplete:(void(^)(BOOL success))complete
{
    NSDictionary *headersDic = [YWCommonTool addSubHttpHeadWithDic:[WMHttpTool shareHttpTool].HTTPHeaders];
    [WMHttpTool shareHttpTool].HTTPHeaders = headersDic;
    if (headersDic == nil) {if (complete) {complete(NO);};return;}
    
    NSDictionary *params = @{@"page" : @(page),
                             @"applyStatus" : @(status)};
    NSString *url = [YWCommonTool materialApplyUrl];
    YWLog(@"param =%@",params);
    [[WMHttpTool shareHttpTool] getWithUrl:url params:params success:^(NSHTTPURLResponse* response, id json) {
        
        NSArray * materialApplys = [YWMaterialApply mj_objectArrayWithKeyValuesArray:json];
//        YWLog(@"%@",json);
        [[YWMaterialManager shareManager] updateMaterialApplyWithArray:materialApplys];
        if (complete) {
            complete(YES);
        }
    } failure:^(NSHTTPURLResponse* response, NSError* error) {
        if (complete) {
            complete(NO);
        }
        if (![[WMHttpTool shareHttpTool] existenceNetwork]) {
            [YWCommonTool showTextHUDWithContent:NSLocalizedString(kTextNoNetwork, nil)];
        }else{
            [YWCommonTool showTextHUDWithContent:@"请求失败,稍后请重试"];
        }
        YWLog(@"%s,error:%@", __func__, error.localizedDescription);
    }];
}
- (void)requestMaterialApplyWithApplyId:(NSInteger)applyId Complete:(void (^)(YWMaterialApply *materialApply))complete{
    NSDictionary *headersDic = [YWCommonTool addSubHttpHeadWithDic:[WMHttpTool shareHttpTool].HTTPHeaders];
    [WMHttpTool shareHttpTool].HTTPHeaders = headersDic;
    if (headersDic == nil) {if (complete) {complete(nil);};return;}
    
    NSString *url = [NSString stringWithFormat:@"%@/%ld",[YWCommonTool materialApplyUrl],(long)applyId];
    [[WMHttpTool shareHttpTool] getWithUrl:url params:nil success:^(NSHTTPURLResponse* response, id json) {
        YWMaterialScanReturnJson *receipientJson = [YWMaterialScanReturnJson mj_objectWithKeyValues:json];
        if (receipientJson.materialApply) {
            [[YWMaterialManager shareManager] updateMaterialApplyWithArray:@[receipientJson.materialApply]];
        }
        if (receipientJson.materialReturn) {
            [[YWMaterialManager shareManager] updateMaterialReturnWithArray:@[receipientJson.materialReturn]];
        }
        if (receipientJson.materialStockOut) {
            [[YWMaterialManager shareManager] updateMaterialReceipientWithArray:@[receipientJson.materialStockOut]];
        }
        [[YWMaterialManager shareManager] updateMaterialCirculationInfoWithArray:receipientJson.materialCirculationInfos];
//        if (receipientJson.materialApply && receipientJson.materialStockOut && receipientJson.materialReturn) {
            YWMaterialRelation *relation = [[YWMaterialRelation alloc] init];
            relation.applyId = receipientJson.materialApply.applyId;
            relation.stockOutId = receipientJson.materialStockOut.stockOutId;
            relation.returnId = receipientJson.materialReturn.returnId;
            [[YWMaterialManager shareManager] updateMaterialRelationWithArray:@[relation]];
//        }

        if (complete) {
            complete(receipientJson.materialApply);
        }
    } failure:^(NSHTTPURLResponse* response, NSError* error) {
        if (complete) {
            complete(nil);
        }
//        if (![[WMHttpTool shareHttpTool] existenceNetwork]) {
//            [YWCommonTool showTextHUDWithContent:NSLocalizedString(kTextNoNetwork, nil)];
//        }
        YWLog(@"%s,error:%@", __func__, error.localizedDescription);
    }];
}

- (void)requestMaterialReceipientListInfoPage:(NSInteger)page Status:(NSInteger)status WithComplete:(void(^)(BOOL success))complete
{
    NSDictionary *headersDic = [YWCommonTool addSubHttpHeadWithDic:[WMHttpTool shareHttpTool].HTTPHeaders];
    [WMHttpTool shareHttpTool].HTTPHeaders = headersDic;
    if (headersDic == nil) {if (complete) {complete(NO);};return;}
    
    NSString * url = [YWCommonTool materialReceipientUrl];
    NSDictionary * params = @{@"page" : @(page),
                              @"stockOutStatus" : @(status)};
    YWLog(@"param =%@",params);
    [[WMHttpTool shareHttpTool] getWithUrl:url params:params success:^(NSHTTPURLResponse* response, id json) {
        NSArray * materialReceipients = [YWMaterialStockOut mj_objectArrayWithKeyValuesArray:json];
        
        [[YWMaterialManager shareManager] updateMaterialReceipientWithArray:materialReceipients];
        if (complete) {
            complete(YES);
        }
    } failure:^(NSHTTPURLResponse* response, NSError* error) {
        if (complete) {
            complete(NO);
        }
        if (![[WMHttpTool shareHttpTool] existenceNetwork]) {
            [YWCommonTool showTextHUDWithContent:NSLocalizedString(kTextNoNetwork, nil)];
        }else{
            [YWCommonTool showTextHUDWithContent:@"请求失败,稍后请重试"];
        }
        YWLog(@"%s,error:%@", __func__, error.localizedDescription);
    }];
}

- (void)requestMaterialBackListInfoPage:(NSInteger)page Status:(NSInteger)status WithComplete:(void(^)(BOOL success))complete
{
    NSDictionary *headersDic = [YWCommonTool addSubHttpHeadWithDic:[WMHttpTool shareHttpTool].HTTPHeaders];
    [WMHttpTool shareHttpTool].HTTPHeaders = headersDic;
    if (headersDic == nil) {if (complete) {complete(NO);};return;}
    
    NSString * url = [YWCommonTool materialBackUrl];
    
    NSDictionary * params = @{@"page" : @(page),
                              @"returnStatus" : @(status)};
    YWLog(@"param =%@",params);
    [[WMHttpTool shareHttpTool] getWithUrl:url params:params success:^(NSHTTPURLResponse* response, id json) {
        NSArray * materialReturns = [YWMaterialReturn mj_objectArrayWithKeyValuesArray:json];
        
        [[YWMaterialManager shareManager] updateMaterialReturnWithArray:materialReturns];

        if (complete) {
            complete(YES);
        }
    } failure:^(NSHTTPURLResponse* response, NSError* error) {
        if (complete) {
            complete(NO);
        }
        if (![[WMHttpTool shareHttpTool] existenceNetwork]) {
            [YWCommonTool showTextHUDWithContent:NSLocalizedString(kTextNoNetwork, nil)];
        }else{
            [YWCommonTool showTextHUDWithContent:@"请求失败,稍后请重试"];
        }
        YWLog(@"%s,error:%@", __func__, error.localizedDescription);
    }];
}
- (void)requestSearchMaterialListWithBuildingId:(NSString *)buildingId SearchCondition:(NSString *)searchCondition page:(NSInteger)page WithComplete:(void(^)(NSArray *searchResult,NSInteger statusCode))complete
{
    NSDictionary *headersDic = [YWCommonTool addSubHttpHeadWithDic:[WMHttpTool shareHttpTool].HTTPHeaders];
    [WMHttpTool shareHttpTool].HTTPHeaders = headersDic;
    if (headersDic == nil) {if (complete) {complete(nil,0);};return;}
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setValue:buildingId?:@"" forKey:@"buildingId"];
    [params setValue:@(page) forKey:@"page"];
    if (searchCondition) {
        [params setValue:searchCondition forKey:@"searchCondition"];
    }
    YWLog(@"%@",params);
    NSString * url = [YWCommonTool materialRearchUrl];
    [[WMHttpTool shareHttpTool] getWithUrl:url params:params success:^(NSHTTPURLResponse *response, id json) {
        
        NSArray * searchArray = [YWMaterialStock mj_objectArrayWithKeyValuesArray:json];
        [[YWMaterialManager shareManager] updateMaterialStockWithArray:searchArray];
        if (complete) {
            complete(searchArray,200);
        }
    } failure:^(NSHTTPURLResponse *response, NSError *error) {
        if (complete) {
            complete(nil,response.statusCode);
        }
//        if (![[WMHttpTool shareHttpTool] existenceNetwork]) {
//            [YWCommonTool showTextHUDWithContent:NSLocalizedString(kTextNoNetwork, nil)];
//        }
        YWLog(@"%s,error:%@", __func__, error.localizedDescription);
    }];
    
}
- (void)scanMaterialReceipientQrcodeWithStockOutId:(NSInteger)stockOutId creatorId:(NSInteger)creatorId isScan:(BOOL)isScan WithComplete:(void (^)(BOOL))complete{
    NSDictionary *headersDic = [YWCommonTool addSubHttpHeadWithDic:[WMHttpTool shareHttpTool].HTTPHeaders];
    [WMHttpTool shareHttpTool].HTTPHeaders = headersDic;
    if (headersDic == nil) {if (complete) {complete(NO);};return;}
    
    NSString* url = [NSString stringWithFormat:@"%@/%ld", [YWCommonTool scanQrcodeReceipientUrl], (long)stockOutId];
    YWLog(@"url = %@",url);
    [[WMHttpTool shareHttpTool] getWithUrl:url params:nil success:^(NSHTTPURLResponse *response, id json) {
     YWMaterialScanReturnJson * materialScanReturnJson = [YWMaterialScanReturnJson mj_objectWithKeyValues:json];
        
        if (materialScanReturnJson.materialApply) {
         [[YWMaterialManager shareManager] updateMaterialApplyWithArray:@[materialScanReturnJson.materialApply]];
        }if (materialScanReturnJson.materialStockOut && creatorId) {
            if (materialScanReturnJson.materialStockOut.creator.uniqueId==0) {
                YWHandler *creator = [[YWHandler alloc] init];
                creator.uniqueId = creatorId;
                materialScanReturnJson.materialStockOut.creator = creator;
            }
            if (materialScanReturnJson.materialStockOut.receiver.uniqueId==0) {
                YWHandler *reciever = [[YWHandler alloc] init];
                reciever.uniqueId = [YWLoginTool loginUser].uniqueId;
                materialScanReturnJson.materialStockOut.receiver = reciever;
                materialScanReturnJson.materialStockOut.stockOutTime = materialScanReturnJson.materialStockOut.updateTime;
            }
//            if (isScan) {
//                materialScanReturnJson.materialStockOut.stockOutStatus = 1;
//            }
            if (materialScanReturnJson.materialStockOut.materialApply==nil) {
                materialScanReturnJson.materialStockOut.materialApply = materialScanReturnJson.materialApply;
            }
            [[YWMaterialManager shareManager] updateMaterialReceipientWithArray:@[materialScanReturnJson.materialStockOut]];
        }
        if (materialScanReturnJson.materialReturn) {
           [[YWMaterialManager shareManager] updateMaterialReturnWithArray:@[materialScanReturnJson.materialReturn]];  
        }
        [[YWMaterialManager shareManager] updateMaterialCirculationInfoWithArray:materialScanReturnJson.materialCirculationInfos];
//        if (materialScanReturnJson.materialApply && materialScanReturnJson.materialStockOut && materialScanReturnJson.materialReturn) {
            YWMaterialRelation *relation = [[YWMaterialRelation alloc] init];
            relation.applyId = materialScanReturnJson.materialApply.applyId;
            relation.stockOutId = materialScanReturnJson.materialStockOut.stockOutId;
            relation.returnId = materialScanReturnJson.materialReturn.returnId;
            [[YWMaterialManager shareManager] updateMaterialRelationWithArray:@[relation]];
//        }
        if (complete) {
            complete(YES);
        }
    } failure:^(NSHTTPURLResponse *response, NSError *error) {
        if (![[WMHttpTool shareHttpTool] existenceNetwork]) {
            [YWCommonTool showTextHUDWithContent:NSLocalizedString(kTextNoNetwork, nil)];
        }else{
            [YWCommonTool showTextHUDWithContent:@"请求领用单失败，请重新扫描"];
        }
        if (complete) {
            complete(NO);
        }
        YWLog(@"%s,error:%@", __func__, error.localizedDescription);
    }];
}

- (void)scanMaterialReturnQrcodeWithReturnId:(NSInteger)returnId creatorId:(NSInteger)creatorId isScan:(BOOL)isScan WithComplete:(void (^)(BOOL))complete
{
    NSDictionary *headersDic = [YWCommonTool addSubHttpHeadWithDic:[WMHttpTool shareHttpTool].HTTPHeaders];
    [WMHttpTool shareHttpTool].HTTPHeaders = headersDic;
    if (headersDic == nil) {if (complete) {complete(NO);};return;}
    
    NSString* url = [NSString stringWithFormat:@"%@/%ld", [YWCommonTool scanQrcodeBackUrl], (long)returnId];
    [[WMHttpTool shareHttpTool] getWithUrl:url params:nil success:^(NSHTTPURLResponse *response, id json) {
        YWMaterialScanReturnJson * materialScanReturnJson = [YWMaterialScanReturnJson mj_objectWithKeyValues:json];
        
        if (materialScanReturnJson.materialApply) {
            [[YWMaterialManager shareManager] updateMaterialApplyWithArray:@[materialScanReturnJson.materialApply]];
        }if (materialScanReturnJson.materialStockOut) {
            if (materialScanReturnJson.materialStockOut.materialApply==nil) {
                materialScanReturnJson.materialStockOut.materialApply = materialScanReturnJson.materialApply;
            }
            [[YWMaterialManager shareManager] updateMaterialReceipientWithArray:@[materialScanReturnJson.materialStockOut]];
        }if (materialScanReturnJson.materialReturn) {
            if (materialScanReturnJson.materialReturn.creator.uniqueId==0) {
                YWHandler *creator = [[YWHandler alloc] init];
                creator.uniqueId = creatorId;
                materialScanReturnJson.materialReturn.creator = creator;
            }
            
            if (materialScanReturnJson.materialReturn.returner.uniqueId==0) {
                YWHandler *returner = [[YWHandler alloc] init];
                returner.uniqueId = [YWLoginTool loginUser].uniqueId;
                materialScanReturnJson.materialReturn.returner = returner;
                materialScanReturnJson.materialReturn.returnTime = materialScanReturnJson.materialReturn.updateTime;
            }
//            if (isScan) {
//                materialScanReturnJson.materialReturn.returnStatus = 2;
//            }
            if (materialScanReturnJson.materialReturn.materialStockOut==nil) {
                materialScanReturnJson.materialReturn.materialStockOut = materialScanReturnJson.materialStockOut;
            }
            [[YWMaterialManager shareManager] updateMaterialReturnWithArray:@[materialScanReturnJson.materialReturn]];
        }
        [[YWMaterialManager shareManager] updateMaterialCirculationInfoWithArray:materialScanReturnJson.materialCirculationInfos];
//        if (materialScanReturnJson.materialApply && materialScanReturnJson.materialStockOut && materialScanReturnJson.materialReturn) {
            YWMaterialRelation *relation = [[YWMaterialRelation alloc] init];
            relation.applyId = materialScanReturnJson.materialApply.applyId;
            relation.stockOutId = materialScanReturnJson.materialStockOut.stockOutId;
            relation.returnId = materialScanReturnJson.materialReturn.returnId;
            [[YWMaterialManager shareManager] updateMaterialRelationWithArray:@[relation]];
//        }
        if (complete) {
            complete(YES);
        }
    } failure:^(NSHTTPURLResponse *response, NSError *error) {
        if (complete) {
            complete(NO);
        }
        if (![[WMHttpTool shareHttpTool] existenceNetwork]) {
            [YWCommonTool showTextHUDWithContent:NSLocalizedString(kTextNoNetwork, nil)];
        }else{
            [YWCommonTool showTextHUDWithContent:@"请求归还单失败，请重新扫描"];
        }
        YWLog(@"%s,error:%@", __func__, error.localizedDescription);
    }];
}

- (void)materialApplyDeleteWithApplyId:(NSInteger)applyId WithComplete:(void (^)(NSInteger stateCode))complete
{
    NSDictionary *headersDic = [YWCommonTool addSubHttpHeadWithDic:[WMHttpTool shareHttpTool].HTTPHeaders];
    [WMHttpTool shareHttpTool].HTTPHeaders = headersDic;
    if (headersDic == nil) {if (complete) {complete(0);};return;}
    
     NSString* url = [NSString stringWithFormat:@"%@/%ld", [YWCommonTool cancelMaterialApplyUrl], (long)applyId];
    [[WMHttpTool shareHttpTool] deleteWithUrl:url params:nil success:^(NSHTTPURLResponse *response, id json) {
        //撤回成功
        if (complete) {
            complete(200);
        }
    } failure:^(NSHTTPURLResponse *response, NSError *error) {
        if (complete) {
            complete(response.statusCode);
        }
        YWLog(@"%s,error:%@", __func__, error.localizedDescription);
    }];
}

- (void)materialAddMoreCopiesWithApplyId:(NSInteger)applyId CarbonCopies:(NSArray *)copyIds Note:(NSString *)note WithComplete:(void (^)(BOOL))complete
{
    NSDictionary *headersDic = [YWCommonTool addSubHttpHeadWithDic:[WMHttpTool shareHttpTool].HTTPHeaders];
    [WMHttpTool shareHttpTool].HTTPHeaders = headersDic;
    if (headersDic == nil) {if (complete) {complete(NO);}return;}
    NSString * url = [YWCommonTool addMoreCopierUrl];
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setObject:@(applyId) forKey:@"applyId"];
    NSMutableString * idString = [NSMutableString string];
    [copyIds enumerateObjectsUsingBlock:^(YWHandler * _Nonnull handler, NSUInteger idx, BOOL * _Nonnull stop) {
        if (idx!=0) {
            [idString appendString:@","];
        }
        [idString appendString:[NSString stringWithFormat:@"%ld",(long)handler.uniqueId]];
    }];
    [params setObject:idString forKey:@"carbonCopy"];
    [params setObject:note forKey:@"note"];
    [[WMHttpTool shareHttpTool] postWithUrl:url params:params success:^(NSHTTPURLResponse *response, id json) {
        if (complete) {
            complete(YES);
        }
    } failure:^(NSHTTPURLResponse *response, NSError *error) {
        if (complete) {
            complete(NO);
        }
        YWLog(@"%s,error:%@", __func__, error.localizedDescription);
    }];
}

- (void)createMaterialApplyWithBuildingId:(NSString *)buildingId Receivers:(NSArray *)recipients CarbonCopy:(NSArray *)carbonCopys Note:(NSString *)note MaterialInfo:(NSArray *)details WithComplete:(void (^)(BOOL))complete
{
    NSDictionary *headersDic = [YWCommonTool addSubHttpHeadWithDic:[WMHttpTool shareHttpTool].HTTPHeaders];
    [WMHttpTool shareHttpTool].HTTPHeaders = headersDic;
    if (headersDic == nil) {if (complete) {complete(NO);}return;}
    NSMutableDictionary* params = [NSMutableDictionary dictionary];
    [params setObject:buildingId forKey:@"buildingId"];

    NSMutableString * receiverString = [NSMutableString string];
    [recipients enumerateObjectsUsingBlock:^(YWHandler * _Nonnull handler, NSUInteger idx, BOOL * _Nonnull stop) {
        if (idx!=0) {
            [receiverString appendString:@","];
        }
        [params setObject:@(handler.uniqueId) forKey:[NSString stringWithFormat:@"recipients[%lu].uniqueId",(unsigned long)idx]];
    }];
    if (carbonCopys.count) {
        [carbonCopys enumerateObjectsUsingBlock:^(YWHandler * _Nonnull handler, NSUInteger idx, BOOL * _Nonnull stop) {
            [params setObject:@(handler.uniqueId) forKey:[NSString stringWithFormat:@"carbonCopys[%lu].uniqueId",(unsigned long)idx]];
        }];
 
    }
    [params setValue:note?:@"" forKey:@"note"];
    [details enumerateObjectsUsingBlock:^(YWMaterialStock*  _Nonnull materialStock, NSUInteger idx, BOOL * _Nonnull stop) {
        [params setObject:@(materialStock.stockId) forKey:[NSString stringWithFormat:@"details[%lu].materialStock.stockId",(unsigned long)idx]];
        [params setObject:@(materialStock.selectQuantity) forKey:[NSString stringWithFormat:@"details[%lu].quantity",(unsigned long)idx]];
    }];
    NSString * url = [YWCommonTool createMaterialApplyUrl];
    YWLog(@"%@",params);
    [[WMHttpTool shareHttpTool] postWithUrl:url params:params success:^(NSHTTPURLResponse *response, id json) {
        YWLog(@"%ld",(long)response.statusCode);
        if (complete) {
            complete(YES);
        }
        
    } failure:^(NSHTTPURLResponse *response, NSError *error) {
        if (![[WMHttpTool shareHttpTool] existenceNetwork]) {
            [YWCommonTool showTextHUDWithContent:NSLocalizedString(kTextNoNetwork, nil)];
        }else{
            [YWCommonTool showTextHUDWithContent:@"请求失败,稍后请重试"];
        }
        if (complete) {
            complete(NO);
        }
        YWLog(@"%s,error:%@", __func__, error.localizedDescription);
    }];
    
    
}
- (void)certurnMaterialStockoutWithBuildingId:(NSString*)buildingId CreatorId:(NSInteger)creatorId StockOutId:(NSInteger)stockOutId WithComplete:(void(^)(BOOL success))complete
{
    NSDictionary *headersDic = [YWCommonTool addSubHttpHeadWithDic:[WMHttpTool shareHttpTool].HTTPHeaders];
    [WMHttpTool shareHttpTool].HTTPHeaders = headersDic;
    if (headersDic == nil) {if (complete) {complete(NO);}return;}
    NSString* url = [YWCommonTool certurnMaterialStockOutUrl];
    NSMutableDictionary* params = [NSMutableDictionary dictionary];
    [params setObject:buildingId?:@"" forKey:@"buildingId"];
    [params setValue:@(creatorId) forKey:@"creatorId"];
    [params setValue:@(stockOutId) forKey:@"stockOutId"];
    [[WMHttpTool shareHttpTool] postWithUrl:url params:params success:^(NSHTTPURLResponse *response, id json) {
        if (complete) {
            complete(YES);
        }
    } failure:^(NSHTTPURLResponse *response, NSError *error) {
        if (complete) {
            complete(NO);
        }
        YWLog(@"%s,error:%@", __func__, error.localizedDescription);
    }];
}

- (void)certurnMaterialReturnWithBuildingId:(NSString*)buildingId CreatorId:(NSInteger)creatorId ReturnId:(NSInteger)returnId WithComplete:(void(^)(BOOL success))complete
{
    NSDictionary *headersDic = [YWCommonTool addSubHttpHeadWithDic:[WMHttpTool shareHttpTool].HTTPHeaders];
    [WMHttpTool shareHttpTool].HTTPHeaders = headersDic;
    if (headersDic == nil) {if (complete) {complete(NO);}return;}
    NSString* url = [YWCommonTool certurnMaterialReturnUrl];
    NSMutableDictionary* params = [NSMutableDictionary dictionary];
    [params setObject:buildingId?:@"" forKey:@"buildingId"];
    [params setValue:@(creatorId) forKey:@"creatorId"];
    [params setValue:@(returnId) forKey:@"returnId"];
    YWLog(@"params = %@",params);
    [[WMHttpTool shareHttpTool] postWithUrl:url params:params success:^(NSHTTPURLResponse *response, id json) {
        if (complete) {
            complete(YES);
        }
    } failure:^(NSHTTPURLResponse *response, NSError *error) {
        if (complete) {
            complete(NO);
        }
        YWLog(@"%s,error:%@", __func__, error.localizedDescription);
    }];
}

- (void)commitMaterialReplyContent:(NSString*)content atUser:(YWHandler*)atUser Type:(NSInteger)type RelationId:(NSInteger)relationId WithComplete:(void(^)(BOOL success))complete
{
    NSDictionary *headersDic = [YWCommonTool addSubHttpHeadWithDic:[WMHttpTool shareHttpTool].HTTPHeaders];
    [WMHttpTool shareHttpTool].HTTPHeaders = headersDic;
    if (headersDic == nil) {if (complete) {complete(NO);}return;}
    NSString* url = [YWCommonTool postReplyUrl];
    NSMutableDictionary* params = [NSMutableDictionary dictionary];
    [params setValue:content?:@"" forKey:@"content"];
    [params setObject:@(atUser.uniqueId) forKey:[NSString stringWithFormat:@"atUser.uniqueId"]];
    [params setValue:@(type) forKey:@"type"];
    [params setValue:@(relationId) forKey:@"relationId"];
    YWLog(@"params = %@",params);
    [[WMHttpTool shareHttpTool] postWithUrl:url params:params success:^(NSHTTPURLResponse *response, id json) {
        YWLog(@"json = %@",json);
        YWReply *reply = [[YWReply alloc] init];
        reply.replyId = [[json objectForKey:@"replyId"] integerValue];
        reply.replyTime = [[json objectForKey:@"replyTime"] longLongValue];
        
        reply.content = content;
        
        reply.atUser = atUser;
        reply.relationId = relationId;
        reply.relationType = type;
        
        YWHandler *replier = [[YWHandler alloc] init];
        replier.uniqueId = [YWLoginTool loginUser].uniqueId;
        reply.replier = replier;
        reply.status = 0;
        reply.isRead = YES;
        
        [[YWReplyManager shareManager] updateReplyWithReplyArray:@[reply]];
        if (complete) {
            complete(YES);
        }
    } failure:^(NSHTTPURLResponse *response, NSError *error) {
        if (![[WMHttpTool shareHttpTool] existenceNetwork]) {
            [YWCommonTool showTextHUDWithContent:NSLocalizedString(kTextNoNetwork, nil)];
        }else{
            [YWCommonTool showTextHUDWithContent:@"提交失败,请稍后重试"];
        }
        if (complete) {
            complete(NO);
        }
        YWLog(@"%s,error:%@", __func__, error.localizedDescription);
    }];
}

- (void)postMaterialStockOutCerturnWithReceiver:(YWHandler*)receiver stockOut:(YWMaterialStockOut*)stockOut WithComplete:(void(^)(BOOL success))complete{
    NSDictionary *headersDic = [YWCommonTool addSubHttpHeadWithDic:[WMHttpTool shareHttpTool].HTTPHeaders];
    [WMHttpTool shareHttpTool].HTTPHeaders = headersDic;
    if (headersDic == nil) {if (complete) {complete(NO);}return;}
    NSString* url = [YWCommonTool noScanMaterialStockOutUrl];
    NSMutableDictionary* params = [NSMutableDictionary dictionary];
    [params setValue:@(stockOut.stockOutId) forKey:@"stockOutId"];
    [params setValue:@(receiver.uniqueId) forKey:@"receiver.uniqueId"];
    NSMutableArray *tempArray = [NSMutableArray array];
    [stockOut.details enumerateObjectsUsingBlock:^(YWMaterialStockOutDetail *_Nonnull stockOutDetail, NSUInteger idx, BOOL * _Nonnull stop) {
        if (stockOutDetail.quantity>0) {
            [tempArray addObject:stockOutDetail];
        }
    }];
    [tempArray enumerateObjectsUsingBlock:^(YWMaterialStockOutDetail * _Nonnull detail, NSUInteger idx, BOOL * _Nonnull stop) {
        [params setValue:@(detail.detailId) forKey:[NSString stringWithFormat:@"details[%lu].detailId",(unsigned long)idx]];
        [params setValue:@(detail.quantity) forKey:[NSString stringWithFormat:@"details[%lu].quantity",(unsigned long)idx]];
    }];
    YWLog(@"params = %@",params);
    [[WMHttpTool shareHttpTool] postWithUrl:url params:params success:^(NSHTTPURLResponse *response, id json) {
        if (complete) {
            complete(YES);
        }
    } failure:^(NSHTTPURLResponse *response, NSError *error) {
        if (complete) {
            complete(NO);
        }
        YWLog(@"%s,error:%@", __func__, error.localizedDescription);
    }];
}

- (void)postMaterialReturnBackCerturnWithCreator:(YWHandler*)creator returnBack:(YWMaterialReturn*)returnBack WithComplete:(void(^)(BOOL success))complete{
    NSDictionary *headersDic = [YWCommonTool addSubHttpHeadWithDic:[WMHttpTool shareHttpTool].HTTPHeaders];
    [WMHttpTool shareHttpTool].HTTPHeaders = headersDic;
    if (headersDic == nil) {if (complete) {complete(NO);}return;}
    NSString* url = [YWCommonTool noScanMaterialReturnBackUrl];
    NSMutableDictionary* params = [NSMutableDictionary dictionary];
    [params setValue:@(returnBack.returnId) forKey:@"returnId"];
    [params setValue:@(creator.uniqueId) forKey:@"creator.uniqueId"];
    NSMutableArray *tempArray = [NSMutableArray array];
    [returnBack.details enumerateObjectsUsingBlock:^(YWMaterialReturnDetail *_Nonnull returnDetail, NSUInteger idx, BOOL * _Nonnull stop) {
        if (returnDetail.quantity>0) {
            [tempArray addObject:returnDetail];
        }
    }];
    [tempArray enumerateObjectsUsingBlock:^(YWMaterialReturnDetail * _Nonnull detail, NSUInteger idx, BOOL * _Nonnull stop) {
        [params setValue:@(detail.detailId) forKey:[NSString stringWithFormat:@"details[%lu].detailId",(unsigned long)idx]];
        [params setValue:@(detail.quantity) forKey:[NSString stringWithFormat:@"details[%lu].quantity",(unsigned long)idx]];
    }];
    YWLog(@"params = %@",params);
    [[WMHttpTool shareHttpTool] postWithUrl:url params:params success:^(NSHTTPURLResponse *response, id json) {
        if (complete) {
            complete(YES);
        }
    } failure:^(NSHTTPURLResponse *response, NSError *error) {
        if (complete) {
            complete(NO);
        }
        YWLog(@"%s,error:%@", __func__, error.localizedDescription);
    }];
}

- (void)postMaterialStockOutReceiveCerturnWithStockOutId:(NSInteger)stockOutId WithComplete:(void(^)(BOOL success))complete{
    NSDictionary *headersDic = [YWCommonTool addSubHttpHeadWithDic:[WMHttpTool shareHttpTool].HTTPHeaders];
    [WMHttpTool shareHttpTool].HTTPHeaders = headersDic;
    if (headersDic == nil) {if (complete) {complete(NO);}return;}
    NSString* url = [YWCommonTool noScanCerturnMaterialStockOutUrl];
    NSMutableDictionary* params = [NSMutableDictionary dictionary];
    [params setValue:@(stockOutId) forKey:@"stockOutId"];
    YWLog(@"params = %@",params);
    [[WMHttpTool shareHttpTool] postWithUrl:url params:params success:^(NSHTTPURLResponse *response, id json) {
        if (complete) {
            complete(YES);
        }
    } failure:^(NSHTTPURLResponse *response, NSError *error) {
        if (complete) {
            complete(NO);
        }
        YWLog(@"%s,error:%@", __func__, error.localizedDescription);
    }];
}

- (void)postMaterialStockOutRejectWithStockOutId:(NSInteger)stockOutId note:(NSString*)note WithComplete:(void(^)(BOOL success))complete{
    NSDictionary *headersDic = [YWCommonTool addSubHttpHeadWithDic:[WMHttpTool shareHttpTool].HTTPHeaders];
    [WMHttpTool shareHttpTool].HTTPHeaders = headersDic;
    if (headersDic == nil) {if (complete) {complete(NO);}return;}
    NSString* url = [YWCommonTool noScanRejectMaterialStockOutUrl];
    NSMutableDictionary* params = [NSMutableDictionary dictionary];
    [params setValue:@(stockOutId) forKey:@"stockOutId"];
    [params setValue:note?:@"" forKey:@"note"];
    YWLog(@"params = %@",params);
    [[WMHttpTool shareHttpTool] postWithUrl:url params:params success:^(NSHTTPURLResponse *response, id json) {
        if (complete) {
            complete(YES);
        }
    } failure:^(NSHTTPURLResponse *response, NSError *error) {
        if (complete) {
            complete(NO);
        }
        YWLog(@"%s,error:%@", __func__, error.localizedDescription);
    }];
}

- (void)postMaterialReturnBackCerturnWithReturnId:(NSInteger)returnId WithComplete:(void(^)(BOOL success))complete{
    NSDictionary *headersDic = [YWCommonTool addSubHttpHeadWithDic:[WMHttpTool shareHttpTool].HTTPHeaders];
    [WMHttpTool shareHttpTool].HTTPHeaders = headersDic;
    if (headersDic == nil) {if (complete) {complete(NO);}return;}
    NSString* url = [YWCommonTool noScanCerturnMaterialReturnBackUrl];
    NSMutableDictionary* params = [NSMutableDictionary dictionary];
    [params setValue:@(returnId) forKey:@"returnId"];
    YWLog(@"params = %@",params);
    [[WMHttpTool shareHttpTool] postWithUrl:url params:params success:^(NSHTTPURLResponse *response, id json) {
        if (complete) {
            complete(YES);
        }
    } failure:^(NSHTTPURLResponse *response, NSError *error) {
        if (complete) {
            complete(NO);
        }
        YWLog(@"%s,error:%@", __func__, error.localizedDescription);
    }];
}

- (void)postMaterialReturnBackRejectWithReturnId:(NSInteger)returnId note:(NSString*)note WithComplete:(void(^)(BOOL success))complete{
    NSDictionary *headersDic = [YWCommonTool addSubHttpHeadWithDic:[WMHttpTool shareHttpTool].HTTPHeaders];
    [WMHttpTool shareHttpTool].HTTPHeaders = headersDic;
    if (headersDic == nil) {if (complete) {complete(NO);}return;}
    NSString* url = [YWCommonTool noScanRejectMaterialReturnBackUrl];
    NSMutableDictionary* params = [NSMutableDictionary dictionary];
    [params setValue:@(returnId) forKey:@"returnId"];
    [params setValue:note?:@"" forKey:@"note"];
    YWLog(@"params = %@",params);
    [[WMHttpTool shareHttpTool] postWithUrl:url params:params success:^(NSHTTPURLResponse *response, id json) {
        if (complete) {
            complete(YES);
        }
    } failure:^(NSHTTPURLResponse *response, NSError *error) {
        if (complete) {
            complete(NO);
        }
        YWLog(@"%s,error:%@", __func__, error.localizedDescription);
    }];
}

@end
